<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * 
 * Description of contacto
 *
 * @author Federico & Leonel
 */
class Protoweb_controller extends Controller {

  public function __construct() {
    parent::__construct();
  } // end of method __construct()

  private function app_path() {
    return (BASEPATH . "application/");
  }

  private function config_path() {
    return ($this->app_path() . "config/");
  }

  private function controllers_path() {
    return ($this->app_path() . "controllers/");
  }

  private function models_path() {
    return ($this->app_path() . "models/");
  }

  private function views_path() {
    return ($this->app_path() . "views/");
  }

  public function get_widget ($id) {
    $widget = new Widget($id);

    $widget_attrs = array();
    foreach ($widget->widget_attribute as $w_attr) {
      $widget_attrs[$w_attr->key] = $w_attr->value;
    }

    include_once $this->config->item("plugins_path") . $widget->plugin->key_name . "/" . $widget->plugin->key_name . ".php";

    $plg = new $widget->plugin->key_name($widget_attrs);

    return($plg);
  }

  public function generate_view_files($action_id) {
    $data["action"]  = new Action($action_id);
    $data["entity"]  = new Entity($data["action"]->entity->id);
    $data["widgets"] = array();

    foreach ($data["action"]->widget->order_by("order")->get() as $widget) {
      array_push($data["widgets"], $this->get_widget($widget->id));
    }

    $this->smarty_parser->assign($data);

    if ($data["action"]->purpose == "list")
      $view_generated = $this->smarty_parser->fetch($this->config->item("generators_path") . "view_template_listing.html");
    else
      $view_generated = $this->smarty_parser->fetch($this->config->item("generators_path") . "view_template_presentation.html");

    $file_path      = $data["entity"]->project->views_path() . $data["entity"]->model_name . "_" . $data["action"]->name . ".html";

    if ( ! write_file($file_path, $view_generated, "w"))
      return(FALSE);
    else
      chmod($file_path,0777);

    return(TRUE);
  } // end of method generate_view_files($action_id)


  /**
   * Generates the model files for the generated project
   *
   * @param integer $id ID of the entity we need to generate model files for
   * @return boolean
   */
  public function generate_model_files($id,$generate_custom_model) {
    $data["entity"]          = new Entity($id);
    $data["has_validations"] = FALSE;

    //checking if at least one attribute has rules
    foreach ($data["entity"]->attribute as $attr) {
      if (strlen($attr->rules) > 0)
        $data["has_validations"] = TRUE;
    }

    $this->smarty_parser->assign($data);

    // entity_template_model_protoweb
    $model_generated = $this->smarty_parser->fetch($this->config->item("generators_path") . "entity_template_model_protoweb.html");
    if ( ! write_file($data["entity"]->project->models_path() . $data["entity"]->model_name . "_protoweb.php", $model_generated, "w"))
      return(FALSE);
    else
      chmod($data["entity"]->project->models_path() . $data["entity"]->model_name . "_protoweb.php", 0777);

    if ($generate_custom_model) {
      // entity_template_model_custom
      $model_generated = $this->smarty_parser->fetch($this->config->item("generators_path")."entity_template_model_custom.html");
      if ( ! write_file($data["entity"]->project->models_path() . $data["entity"]->model_name . ".php", $model_generated, "w"))
        return(FALSE);
      else
        chmod($data["entity"]->project->models_path() . $data["entity"]->model_name . ".php", 0777);
    }

    return(TRUE);

  } // end of method generate_model_files($id)

  public function generate_controller_files($entity_id,$generate_custom_controller) {
    $data["entity"] = new Entity($entity_id);

    $this->smarty_parser->assign($data);

    $controller_generated = $this->smarty_parser->fetch($this->config->item("generators_path") . "controller_template_protoweb.html");

    if ( ! write_file($data["entity"]->project->controllers_path() . $data["entity"]->table_name . "_protoweb.php", $controller_generated, "w"))
      return(FALSE);
    else
      chmod($data["entity"]->project->controllers_path() . $data["entity"]->table_name . "_protoweb.php", 0777);

    if ($generate_custom_controller) {
      // entity_template_controller_custom
      $controller_generated = $this->smarty_parser->fetch($this->config->item("generators_path")."controller_template_custom.html");
      if ( ! write_file($data["entity"]->project->controllers_path() . $data["entity"]->table_name . ".php", $controller_generated, "w"))
        return(FALSE);
      else
        chmod($data["entity"]->project->controllers_path() . $data["entity"]->table_name . ".php", 0777);
    }

    /**
     * generating edit view
     */
    $view_generated = $this->smarty_parser->fetch($this->config->item("generators_path") . "view_template_form.html");

    if ( ! write_file($data["entity"]->project->views_path() . $data["entity"]->model_name . "_edit.html", $view_generated, "w"))
      return(FALSE);
    else
      chmod($data["entity"]->project->views_path() . $data["entity"]->model_name . "_edit.html", 0777);


    return(TRUE);
  }// end of method generate_controller

} // class Protoweb

/* End of file protoweb.php */
/* Location: ./system/application/controllers/protoweb.php */
